package cn.edu.jmu.oelp.controller;

import cn.edu.jmu.oelp.pojo.Paragraphs;
import cn.edu.jmu.oelp.pojo.RespCode;
import cn.edu.jmu.oelp.pojo.RespEntity;
import cn.edu.jmu.oelp.service.EntityNeo4jService;
import cn.edu.jmu.oelp.service.EntityService;
import cn.edu.jmu.oelp.service.ParagraphsService;
import cn.edu.jmu.oelp.service.TypeService;
import cn.edu.jmu.oelp.utils.AnsjUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.PublicKey;

/**
 * 前端用户请求标注后，查询是否有对应的MQ，若没有则创建对应的队列
 * @author Wen
 */
@RestController
@Transactional(rollbackFor=Exception.class)
public class ParagraphsController {
    @Autowired
    ParagraphsService paragraphsService;

    @Autowired
    EntityService entityService;

    @Autowired
    TypeService typeService;

    @Autowired
    AnsjUtils ansjUtils;

    @Autowired
    EntityNeo4jService entityNeo4jService;

    /**
     * @author Wen
     * @param id 用户id
     * @param parId 段落id
     * @return 段落类的json
     */
    @Deprecated
    @RequestMapping("/nextPar")
    public Paragraphs getNextParagraphs(int id,int parId){
        Paragraphs paragraphs;
        if (parId==0){
            int paragraphsId = entityService.lastParagraphsId(id);
            paragraphs = paragraphsService.findUserNextParagraphs(paragraphsId);
        }else {
            paragraphs = paragraphsService.findUserNextParagraphs(parId);
        }
        ansjUtils.setText(paragraphs.getContent());
        ansjUtils.toAnalysis();
        paragraphs.setEntityList(ansjUtils.getList());
        paragraphs.setTypeList(typeService.findTypes(paragraphs.getArticleId()));
        return paragraphs;
    }

    /**
     * @author damao
     * @param userId
     * @param parId
     * @return 返回时的段落里有标注entities
     */
    @RequestMapping("/getNextPar")
    public Paragraphs getNextParagraphsWithEntity(int userId, int parId){
        Paragraphs paragraphs;
        if (parId==0){
            int paragraphsId = entityService.lastParagraphsId(userId);
            paragraphs = paragraphsService.findUserNextParagraphs(paragraphsId);
        }else {
            paragraphs = paragraphsService.findUserNextParagraphs(parId);
        }

        return getParagraphs(userId, paragraphs);
    }
    /**
     * @author Wen
     * @param userId
     * @param parId
     * @return 返回时的段落里有标注entities,并且返回的是上一段的段落
     */
    @RequestMapping("/getBackPar")
    public Paragraphs getBackParagraphsWithEntity(int userId,int parId){
        Paragraphs paragraphs;
        paragraphs = paragraphsService.findUserBackParagraphs(parId);

        return getParagraphs(userId, paragraphs);
    }

    public Paragraphs getParagraphs(int userId, Paragraphs paragraphs) {
        ansjUtils.setText(paragraphs.getContent());
        ansjUtils.toAnalysis();
        paragraphs.setEntityList(ansjUtils.getList());
        paragraphs.setTypeList(typeService.findTypes(paragraphs.getArticleId()));
        paragraphs.setEntities(entityNeo4jService.findByArticleIdAndParagraphsIdAndUserId(paragraphs.getArticleId(), paragraphs.getParagraphsId(), userId));
        return paragraphs;
    }


}
